﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comm;

public partial class admin_head : LoginBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindImage();
        }
    }

    public void BindImage()
    {
        HeadImage.ImageUrl = base.GetImage(base.User.Pic);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string[] allowExt = new string[] { ".jpg", ".png", ".gif", ".bmp" };
        string exten = System.IO.Path.GetExtension(fileupImage.FileName).ToLower();
        bool isallow = false;
        foreach (string s in allowExt)
        {
            if (s.Equals(exten)) isallow = true; break;
        }
        if (isallow)
        {
            Model.User u = new Model.User();
            string Pic = (new Dispose_Image()).upLoadImage(fileupImage, string.Format("/UploadImage/{0}/", base.User.ID));
            if (!string.IsNullOrEmpty(Pic))
            {

                u.Pic = (new Dispose_Image()).upLoadThumbnail(Server.MapPath(Pic), string.Format("/UploadImage/PersonalityImg/{0}/Head/", base.User.ID), 150, 150).Replace("//", "/");
                if (new BLL.User().Update(u, "ID=" + base.User.ID) > 0)
                {
                    Model.User uu = base.User;
                    //删除老图片
                    System.IO.File.Delete(Server.MapPath(uu.Pic));
                    //更新图片
                    uu.Pic = u.Pic;
                    base.User = uu;
                    CnGine.Comm.MessageBox.AlertAndRedirect("上传图片成功！", "head.aspx", this);
                }
                else
                {
                    CnGine.Comm.MessageBox.Alert("上传头像失败，请重试！", this);
                }

            }
            try
            {
                System.IO.File.Delete(Server.MapPath(Pic));
            }
            catch
            { }
        }
        else
        {
            CnGine.Comm.MessageBox.Alert("图片格式错误，只允许上传 jpg,png,gif,bmp文件。", this);
        }
    }
}